package com.example.gestureactivity;

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class SimActivity extends BaseActivity implements OnClickListener {

	private LinearLayout mLayout;
	private TextView mTvSim;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.activity_simple);

		initView();
	}

	private void initView() {
		mLayout = (LinearLayout) findViewById(R.id.layout_simple);
		Button mBtnSim = (Button) findViewById(R.id.bt_simple);
		Button mBtnSimChild = (Button) findViewById(R.id.btn_simple_child);

		mTvSim = (TextView) findViewById(R.id.tv_simple_show);

		mBtnSim.setOnClickListener(this);
		mBtnSimChild.setOnClickListener(this);
		setListener();

	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		int action = event.getAction();
		switch (action) {
		case MotionEvent.ACTION_DOWN:
			startX = event.getX();
			startY = event.getY();
			break;
		case MotionEvent.ACTION_MOVE:
			break;
		case MotionEvent.ACTION_UP:
		case MotionEvent.ACTION_CANCEL:
			float currentX = event.getX();
			float currentY = event.getY();
			float dx = currentX - startX;
			float dy = currentY - startY;
			if (dx > 0 && Math.abs(dx) > Math.abs(dy)) {
				// 右划，而且水平方向的位移大于竖直方向的位移
				onFlingFinished();
			}
			break;

		}
		return false;
	}

	@Override
	void onFlingFinished() {
		this.finish();
	}

	private void setListener() {
		for (int i = 0; i < mLayout.getChildCount(); i++) {
			mLayout.getChildAt(i).setOnTouchListener(this);
		}
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.bt_simple:
			mTvSim.setText("父控件内的Button被点击了");
			break;
		case R.id.btn_simple_child:
			mTvSim.setText("子控件内的Button被点击了");
			break;
		}
		mTvSim.setVisibility(View.VISIBLE);
		
		mTvSim.postDelayed(new Runnable() {
			
			@Override
			public void run() {
				mTvSim.setVisibility(View.GONE);
			}
		}, 2000);
	}
}
